﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BarrageGrab
{
    public partial class CommandForm : UIForm
    {
        private string mode = "";  //添加，修改
        private Dictionary<string, string> command;

        public CommandForm(string op,Dictionary<string,string> g)
        {
            InitializeComponent();
            mode = op;
            command = g;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string cmd_type = "";
            if (rb1.Checked) {
                cmd_type = rb1.Text;
            }
            else if (rb2.Checked)
            {
                cmd_type = rb2.Text;
            }
            else if (rb3.Checked)
            {
                cmd_type = rb3.Text;
            }
            if (mode == "添加") {
                string insertSql = $"insert into command_config (cmd_hex,cmd_type,cmd_tube) values ('{txtCmdHex.Text}','{cmd_type}','{cbCmdTube.Text}')";
                DbHelperSQLite.ExecuteSql(insertSql);
            }
            else if (mode == "修改")
            {
                string updateSql = $"update command_config set cmd_hex='{txtCmdHex.Text}',cmd_type='{cmd_type}',cmd_tube='{cbCmdTube.Text}' where cmd_hex = '{command["cmd_hex"]}'";
                DbHelperSQLite.ExecuteSql(updateSql);
            }
            this.Close();

        }

        private void GiftForm_Load(object sender, EventArgs e)
        {
            if (command != null) {
                txtCmdHex.Text = command["cmd_hex"];
                cbCmdTube.Text= command["cmd_tube"];
                if (command["cmd_type"] == rb1.Text) { 
                    rb1.Checked = true;
                }
                else if (command["cmd_type"] == rb2.Text)
                {
                    rb2.Checked = true;
                }
                else if (command["cmd_type"] == rb3.Text)
                {
                    rb3.Checked = true;
                }
            }
        }
    }
}
